namespace DotNetNuke.Services.Log.EventLog
{
    using DotNetNuke.Common;
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Framework.Providers;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Mail;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Text;
    using System.Threading;
    using System.Web;
    using System.Web.Caching;
    using System.Xml;

    public class XMLLoggingProvider : LoggingProvider
    {
        private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration("logging");
        private static ReaderWriterLock lockLog = new ReaderWriterLock();
        private static ReaderWriterLock lockNotif = new ReaderWriterLock();
        private static ReaderWriterLock lockPurgeLog = new ReaderWriterLock();
        public static Collection LogQueue = new Collection();
        private const string PendingNotificationsFile = "PendingLogNotifications.xml.resources";
        private const string ProviderType = "logging";
        private const int ReaderLockTimeout = 0x2710;
        private const int WriterLockTimeout = 0x2710;
        private static XmlDocument xmlConfigDoc;

        public XMLLoggingProvider()
        {
            if (xmlConfigDoc == null)
            {
                xmlConfigDoc = this.GetConfigDoc();
            }
        }

        public override void AddLog(LogInfo objLogInfo)
        {
            string ConfigPortalID;
            if (objLogInfo.LogPortalID != Null.NullInteger)
            {
                ConfigPortalID = objLogInfo.LogPortalID.ToString();
            }
            else
            {
                ConfigPortalID = "*";
            }
            LogTypeConfigInfo objLogTypeConfigInfo = this.GetLogTypeConfig(ConfigPortalID, objLogInfo.LogTypeKey);
            if ((((objLogTypeConfigInfo != null) && objLogTypeConfigInfo.LoggingIsActive) ? 1 : 0) != 0)
            {
                objLogInfo.LogFileID = objLogTypeConfigInfo.ID;
                objLogInfo.LogCreateDateNum = this.DateToNum(objLogInfo.LogCreateDate);
                string LogString = XmlUtils.Serialize(objLogInfo);
                LogQueueItem objLogQueueItem = new LogQueueItem();
                objLogQueueItem.LogString = LogString;
                objLogQueueItem.LogTypeConfigInfo = objLogTypeConfigInfo;
                bool UseEventLogBuffer = true;
                if (DotNetNuke.Common.Globals.HostSettings.ContainsKey("EventLogBuffer"))
                {
                    if (Convert.ToString(RuntimeHelpers.GetObjectValue(DotNetNuke.Common.Globals.HostSettings["EventLogBuffer"])) != "Y")
                    {
                        UseEventLogBuffer = false;
                    }
                }
                else
                {
                    UseEventLogBuffer = false;
                }
                SchedulingProvider scheduler = SchedulingProvider.Instance();
                if (((objLogInfo.BypassBuffering | !SchedulingProvider.Enabled) | (scheduler.GetScheduleStatus() == ScheduleStatus.STOPPED)) | !UseEventLogBuffer)
                {
                    this.WriteLog(objLogQueueItem);
                }
                else
                {
                    LogQueue.Add(objLogQueueItem, null, null, null);
                }
            }
        }

        public override void AddLogType(string LogTypeKey, string LogTypeFriendlyName, string LogTypeDescription, string LogTypeCSSClass, string LogTypeOwner)
        {
            XmlDocument xmlDoc = this.GetConfigDoc();
            XmlNode xmlNode = xmlDoc.ImportNode(this.GetXMLFromLogTypeInfo(LogTypeKey, LogTypeFriendlyName, LogTypeDescription, LogTypeCSSClass, LogTypeOwner), true);
            xmlDoc.DocumentElement.AppendChild(xmlNode);
            xmlDoc.Save(this.GetConfigDocFileName());
            DataCache.RemoveCache("LoggingGetConfigDoc");
            xmlConfigDoc = this.GetConfigDoc();
        }

        public override void AddLogTypeConfigInfo(string ID, bool IsActive, string LogTypeKey, string LogTypePortalID, string KeepMostRecent, string LogFileName, bool EmailNotificationIsActive, string NotificationThreshold, string NotificationThresholdTime, string NotificationThresholdTimeType, string MailFromAddress, string MailToAddress)
        {
            XmlDocument xmlDoc = this.GetConfigDoc();
            XmlNode xmlNode = xmlDoc.ImportNode(this.GetXMLFromLogTypeConfigInfo(ID, IsActive, LogTypeKey, LogTypePortalID, KeepMostRecent, LogFileName, EmailNotificationIsActive, NotificationThreshold, NotificationThresholdTime, NotificationThresholdTimeType, MailFromAddress, MailToAddress), true);
            xmlDoc.DocumentElement.AppendChild(xmlNode);
            xmlDoc.Save(this.GetConfigDocFileName());
            DataCache.RemoveCache("LoggingGetConfigDoc");
            xmlConfigDoc = this.GetConfigDoc();
        }

        public override void ClearLog()
        {
            XmlDocument xmlConfigDoc = this.GetConfigDoc();
            ArrayList a = this.GetLogFiles(xmlConfigDoc, "*", "*");
            int refIntHelperL0 = a.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                File.Delete(Convert.ToString(RuntimeHelpers.GetObjectValue(a[i])));
            }
        }

        private long DateToNum(DateTime dt)
        {
            long i = Convert.ToInt64(dt.Year) * 0x9184e72a000L;
            i += Convert.ToInt64(dt.Month) * 0x174876e800L;
            i += Convert.ToInt64(dt.Day) * 0x3b9aca00L;
            i += Convert.ToInt64(dt.Hour) * 0x989680L;
            i += Convert.ToInt64(dt.Minute) * 0x186a0L;
            i += Convert.ToInt64(dt.Second) * 0x3e8L;
            return (i + (Convert.ToInt64(dt.Millisecond) * 1L));
        }

        public override void DeleteLog(LogInfo objLogInfo)
        {
            int intAttempts=0;
            XmlDocument xmlConfigDoc = this.GetConfigDoc();
            string strFileName = this.GetLogFileByLogFileID(this.GetConfigDoc(), objLogInfo.LogFileID);
            XmlDocument xmlDoc = new XmlDocument();
            while (!((xmlDoc.OuterXml != "") || (intAttempts == 100)))
            {
                intAttempts++;
                try
                {
                    xmlDoc.Load(strFileName);
                }
                catch (IOException exception1)
                {
    
                    IOException exc = exception1;
                    Thread.Sleep(1);
    
                }
            }
            XmlNode xmlNode = xmlDoc.SelectSingleNode("/logs/log[@LogGUID='" + objLogInfo.LogGUID + "']");
            if (xmlNode != null)
            {
                xmlDoc.DocumentElement.RemoveChild(xmlNode);
            }
            xmlDoc.Save(strFileName);
        }

        public override void DeleteLogType(string LogTypeKey)
        {
            XmlDocument xmlDoc = this.GetConfigDoc();
            XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode("LogTypes/LogType[@LogTypeKey='" + LogTypeKey + "']");
            if (xmlNode != null)
            {
                xmlDoc.DocumentElement.RemoveChild(xmlNode);
                xmlDoc.Save(this.GetConfigDocFileName());
            }
            DataCache.RemoveCache("LoggingGetConfigDoc");
            xmlConfigDoc = this.GetConfigDoc();
        }

        public override void DeleteLogTypeConfigInfo(string ID)
        {
            XmlDocument xmlDoc = this.GetConfigDoc();
            XmlNode xmlNode = xmlDoc.DocumentElement.SelectSingleNode("LogTypeConfig[@LogFileID='" + ID + "']");
            if (xmlNode != null)
            {
                xmlDoc.DocumentElement.RemoveChild(xmlNode);
                xmlDoc.Save(this.GetConfigDocFileName());
            }
            DataCache.RemoveCache("LoggingGetConfigDoc");
            xmlConfigDoc = this.GetConfigDoc();
        }

        private void DeleteOldPendingNotifications()
        {
            XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
            try
            {
                xmlPendingNotificationsDoc.Load(GetFilePath("PendingLogNotifications.xml.resources"));
            }
            catch (FileNotFoundException exception1)
            {

                FileNotFoundException exc = exception1;

                return;

            }
            DateTime LastNotificationSuccess = Null.NullDate;
            DateTime LastNotificationFailure = Null.NullDate;
            if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"] != null)
            {
                LastNotificationFailure = Conversions.ToDate(xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"].Value);
            }
            if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"] != null)
            {
                LastNotificationSuccess = Conversions.ToDate(xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"].Value);
            }
            if (DateTime.Compare(LastNotificationFailure, LastNotificationSuccess) <= 0)
            {
                XmlDocument xmlConfigDoc = this.GetConfigDoc();
                ArrayList arrLogTypeInfo = this.GetLogTypeConfigInfo();
                int refIntHelperL0 = arrLogTypeInfo.Count - 1;
                for (int a = 0; a <= refIntHelperL0; a++)
                {
                    LogTypeConfigInfo objLogTypeInfo = (LogTypeConfigInfo) arrLogTypeInfo[a];
                    if (objLogTypeInfo.EmailNotificationIsActive)
                    {
                        XmlNodeList xmlPendingNotifications = xmlPendingNotificationsDoc.DocumentElement.SelectNodes("log[@LogTypeKey='" + objLogTypeInfo.LogTypeKey + "' and @LogTypePortalID='" + objLogTypeInfo.LogTypePortalID + "' and @CreateDateNum < '" + this.DateToNum(objLogTypeInfo.StartDateTime).ToString() + "']");
                        if (xmlPendingNotifications.Count > 0)
                        {
                            IEnumerator refObjectHelperL0=null;
                            try
                            {
                                refObjectHelperL0 = xmlPendingNotifications.GetEnumerator();
                                while (refObjectHelperL0.MoveNext())
                                {
                                    XmlNode xmlPendingNotification = (XmlNode) refObjectHelperL0.Current;
                                    xmlPendingNotificationsDoc.DocumentElement.RemoveChild(xmlPendingNotification);
                                }
                            }
                            finally
                            {
                                if (refObjectHelperL0 is IDisposable)
                                {
                                    (refObjectHelperL0 as IDisposable).Dispose();
                                }
                            }
                        }
                    }
                }
                xmlPendingNotificationsDoc.Save(GetFilePath("PendingLogNotifications.xml.resources"));
            }
        }

        private XmlDocument GetConfigDoc()
        {
            if (((XmlDocument) DataCache.GetCache("LoggingGetConfigDoc")) == null)
            {
                int intAttempts=0;
                string strConfigDoc = this.GetConfigDocFileName();
                XmlDocument xmlConfigDoc = new XmlDocument();
                if (!File.Exists(strConfigDoc))
                {
                    File.Copy(DotNetNuke.Common.Globals.HostMapPath + @"Logs\LogConfig\LogConfigTemplate.xml.resources", strConfigDoc);
                    File.SetAttributes(strConfigDoc, FileAttributes.Normal);
                }
                while (!((xmlConfigDoc.OuterXml != "") | (intAttempts == 100)))
                {
                    intAttempts++;
                    try
                    {
                        xmlConfigDoc.Load(strConfigDoc);
                    }
                    catch (IOException exception1)
                    {
        
                        IOException exc = exception1;
                        Thread.Sleep(1);
        
                    }
                }
                string filePath = this.GetConfigDocFileName();
                if (File.Exists(filePath))
                {
                    DataCache.SetCache("LoggingGetConfigDoc", xmlConfigDoc, new CacheDependency(filePath));
                }
                else
                {
                    return xmlConfigDoc;
                }
            }
            return (XmlDocument) DataCache.GetCache("LoggingGetConfigDoc");
        }

        private string GetConfigDocFileName()
        {
            Provider objProvider = (Provider) this._providerConfiguration.Providers[this._providerConfiguration.DefaultProvider];
            return GetFilePath(objProvider.Attributes["configfilename"], @"LogConfig\");
        }

        private string GetConfigProviderPath()
        {
            Provider objProvider = (Provider) this._providerConfiguration.Providers[this._providerConfiguration.DefaultProvider];
            return HttpContext.Current.Server.MapPath(objProvider.Attributes["providerPath"]);
        }

        private static string GetFilePath(string strFileName)
        {
            return GetFilePath(strFileName, "");
        }

        private static string GetFilePath(string strFileName, string strFolder)
        {
            if ((strFileName.LastIndexOf(@"\") == -1) & (strFileName.LastIndexOf("/") == -1))
            {
                HttpContext objHttpContext = HttpContext.Current;
                if (strFolder != "")
                {
                    strFileName = DotNetNuke.Common.Globals.HostMapPath + @"Logs\" + strFolder + strFileName;
                    return strFileName;
                }
                strFileName = DotNetNuke.Common.Globals.HostMapPath + @"Logs\" + strFileName;
            }
            return strFileName;
        }

        public override LogInfoArray GetLog()
        {
            int refIntHelperS0 = 0;
            return this.GetLogFromXPath("logs/log", "*", "*", 0x7fffffff, 1, ref refIntHelperS0);
        }

        public override LogInfoArray GetLog(int PortalID)
        {
            if (PortalID == -1)
            {
                return this.GetLog();
            }
            int refIntHelperS0 = 0;
            return this.GetLogFromXPath("logs/log[@LogPortalID='" + PortalID.ToString() + "']", PortalID.ToString(), "*", 0x7fffffff, 1, ref refIntHelperS0);
        }

        public override LogInfoArray GetLog(string LogType)
        {
            int refIntHelperS0 = 0;
            return this.GetLogFromXPath("logs/log[@LogTypeKey='" + LogType + "']", "*", LogType, 0x7fffffff, 1, ref refIntHelperS0);
        }

        public override LogInfoArray GetLog(int PortalID, string LogTypeKey)
        {
            int refIntHelperS0;
            if (PortalID == -1)
            {
                refIntHelperS0 = 0;
                return this.GetLog(LogTypeKey, 0x7fffffff, 1, ref refIntHelperS0);
            }
            refIntHelperS0 = 0;
            return this.GetLogFromXPath("logs/log[@LogPortalID='" + PortalID.ToString() + "' and @LogTypeKey='" + LogTypeKey + "']", PortalID.ToString(), LogTypeKey, 0x7fffffff, 1, ref refIntHelperS0);
        }

        public override LogInfoArray GetLog(int PageSize, int PageIndex, ref int TotalRecords)
        {
            return this.GetLogFromXPath("logs/log", "*", "*", PageSize, PageIndex, ref TotalRecords);
        }

        public override LogInfoArray GetLog(int PortalID, int PageSize, int PageIndex, ref int TotalRecords)
        {
            if (PortalID == -1)
            {
                return this.GetLog();
            }
            return this.GetLogFromXPath("logs/log[@LogPortalID='" + PortalID.ToString() + "']", PortalID.ToString(), "*", PageSize, PageIndex, ref TotalRecords);
        }

        public override LogInfoArray GetLog(string LogType, int PageSize, int PageIndex, ref int TotalRecords)
        {
            return this.GetLogFromXPath("logs/log[@LogTypeKey='" + LogType + "']", "*", LogType, PageSize, PageIndex, ref TotalRecords);
        }

        public override LogInfoArray GetLog(int PortalID, string LogTypeKey, int PageSize, int PageIndex, ref int TotalRecords)
        {
            if (PortalID == -1)
            {
                return this.GetLog(LogTypeKey, PageSize, PageIndex, ref TotalRecords);
            }
            return this.GetLogFromXPath("logs/log[@LogPortalID='" + PortalID.ToString() + "' and @LogTypeKey='" + LogTypeKey + "']", PortalID.ToString(), LogTypeKey, PageSize, PageIndex, ref TotalRecords);
        }

        private string GetLogFileByLogFileID(XmlDocument xmlConfigDoc, string LogFileID)
        {
            XmlNode xmlLogFile = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogFileID='" + LogFileID + "']/@FileName");
            if (xmlLogFile != null)
            {
                return GetFilePath(xmlLogFile.InnerText);
            }
            return "";
        }

        private ArrayList GetLogFiles(XmlDocument xmlConfigDoc)
        {
            return this.GetLogFiles(xmlConfigDoc, "*", "*");
        }

        private ArrayList GetLogFiles(XmlDocument xmlConfigDoc, string ConfigPortalID)
        {
            return this.GetLogFiles(xmlConfigDoc, ConfigPortalID, "*");
        }

        private ArrayList GetLogFiles(XmlDocument xmlConfigDoc, string ConfigPortalID, string LogTypeKey)
        {
            Hashtable ht = new Hashtable();
            ArrayList arrFiles = new ArrayList();
            XmlNodeList xmlLogFiles = null;
            if ((ConfigPortalID == "*") & (LogTypeKey == "*"))
            {
                xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig/@FileName");
            }
            else if ((ConfigPortalID != "*") & (LogTypeKey == "*"))
            {
                xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig[@LogTypePortalID='" + ConfigPortalID + "']/@FileName");
            }
            else if ((ConfigPortalID == "*") & (LogTypeKey != "*"))
            {
                xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig[@LogTypeKey='" + LogTypeKey + "']/@FileName");
            }
            else if ((ConfigPortalID != "*") & (LogTypeKey != "*"))
            {
                xmlLogFiles = xmlConfigDoc.SelectNodes("/LogConfig/LogTypeConfig[@LogTypePortalID='" + ConfigPortalID + "' and @LogTypeKey='" + LogTypeKey + "']/@FileName");
            }
            if (xmlLogFiles != null)
            {
                IEnumerator refObjectHelperL0=null;
                try
                {
                    refObjectHelperL0 = xmlLogFiles.GetEnumerator();
                    while (refObjectHelperL0.MoveNext())
                    {
                        XmlNode xmlLogFile = (XmlNode) refObjectHelperL0.Current;
                        if ((((xmlLogFile.InnerText != "") && (ht[xmlLogFile.InnerText] == null)) ? 1 : 0) != 0)
                        {
                            arrFiles.Add(GetFilePath(xmlLogFile.InnerText));
                            ht.Add(xmlLogFile.InnerText, true);
                        }
                    }
                }
                finally
                {
                    if (refObjectHelperL0 is IDisposable)
                    {
                        (refObjectHelperL0 as IDisposable).Dispose();
                    }
                }
            }
            XmlNode xmlDefaultLogFile = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey='*' and @LogTypePortalID='*']/@FileName");
            if ((((xmlDefaultLogFile != null) && (ht[xmlDefaultLogFile.InnerText] == null)) ? 1 : 0) != 0)
            {
                arrFiles.Add(GetFilePath(xmlDefaultLogFile.InnerText));
                ht.Add(xmlDefaultLogFile.InnerText, true);
            }
            return arrFiles;
        }

        private LogInfoArray GetLogFromXPath(string xpath, string PortalID, string LogType, int PageSize, int PageIndex, ref int TotalRecords)
        {
            XmlDocument xmlConfigDoc = this.GetConfigDoc();
            ArrayList arrLogFiles = this.GetLogFiles(xmlConfigDoc, PortalID, LogType);
            XmlDocument xmlLogFiles = new XmlDocument();
            xmlLogFiles.LoadXml("<LogCollection></LogCollection>");
            XmlElement xmlLogFilesDocEl = xmlLogFiles.DocumentElement;
            ArrayList arrLogInfo = new ArrayList();
            int refIntHelperL0 = arrLogFiles.Count - 1;
            for (int i = 0; i <= refIntHelperL0; i++)
            {
                bool FileIsCorrupt = false;
                bool FileExists = true;
                string LogFile = Convert.ToString(RuntimeHelpers.GetObjectValue(arrLogFiles[i]));
                XmlDocument xmlLogFile = new XmlDocument();
                try
                {
                    lockLog.AcquireReaderLock(0x2710);
                    xmlLogFile.Load(LogFile);
                }
                catch (FileNotFoundException exception1)
                {
    
                    FileNotFoundException exc = exception1;
                    FileExists = false;
    
                }
                catch (XmlException exception2)
                {
                    ProjectData.SetProjectError(exception2);
                    XmlException exc = exception2;
                    FileExists = false;
                    FileIsCorrupt = true;
    
                }
                finally
                {
                    lockLog.ReleaseReaderLock();
                }
                if (FileIsCorrupt)
                {
                    string s = "A log file is corrupt '" + LogFile + "'.";
                    if (Strings.InStr(LogFile, "Exceptions.xml.resources", CompareMethod.Binary) > 0)
                    {
                        s = s + "  This could be due to an older exception log file being written to by the new logging provider.  Try removing 'Exceptions.xml.resources' from the logs directory to solve the problem.";
                    }
                    LogInfo objEventLogInfo = new LogInfo();
                    objEventLogInfo.AddProperty("Note", s);
                    objEventLogInfo.BypassBuffering = true;
                    objEventLogInfo.LogTypeKey = "HOST_ALERT";
                    new EventLogController().AddLog(objEventLogInfo);
                }
                else if (FileExists)
                {
                    IEnumerator refObjectHelperL0=null;
                    XmlNodeList xmlNodes = xmlLogFile.SelectNodes(xpath);
                    XmlElement xmlLogNodes = xmlLogFiles.CreateElement("logs");
                    try
                    {
                        refObjectHelperL0 = xmlNodes.GetEnumerator();
                        while (refObjectHelperL0.MoveNext())
                        {
                            XmlNode xmlNode = (XmlNode) refObjectHelperL0.Current;
                            xmlLogNodes.AppendChild(xmlLogFiles.ImportNode(xmlNode, true));
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL0 is IDisposable)
                        {
                            (refObjectHelperL0 as IDisposable).Dispose();
                        }
                    }
                    xmlLogFilesDocEl.AppendChild(xmlLogNodes);
                }
            }
            return this.GetLogInfoFromXML(xmlLogFiles, PageSize, PageIndex, ref TotalRecords);
        }

        private LogInfoArray GetLogInfoFromXML(XmlDocument xmlLogFiles, int PageSize, int PageIndex, ref int TotalRecords)
        {
            IEnumerator refObjectHelperL0=null;
            Stream str = new MemoryStream();
            StreamWriter xw = new StreamWriter(str, Encoding.UTF8);
            XmlUtils.XSLTransform(xmlLogFiles, ref xw, this.GetConfigProviderPath() + "log.xslt");
            xw.Flush();
            str.Position = 0L;
            StreamReader x = new StreamReader(str);
            xmlLogFiles.Load(x);
            x.Close();
            TotalRecords = xmlLogFiles.DocumentElement.SelectNodes("logs/log").Count;
            int TotalPages = Convert.ToInt32(Math.Ceiling((double) (((double) TotalRecords) / ((double) PageSize))));
            int LowNum = PageIndex * PageSize;
            int HighNum = (PageIndex * PageSize) + PageSize;
            if (HighNum > TotalRecords)
            {
                HighNum = TotalRecords;
            }
            LogInfoArray arrLog = new LogInfoArray();
            try
            {
                refObjectHelperL0 = xmlLogFiles.DocumentElement.SelectNodes("logs/log[position()>=" + LowNum.ToString() + " and position()<" + HighNum.ToString() + "]").GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    IEnumerator refObjectHelperL1=null;
                    XmlNode XmlNode = (XmlNode) refObjectHelperL0.Current;
                    LogInfo Log = new LogInfo();
                    LogProperties logProp = new LogProperties();
                    if (XmlNode.Attributes["LogTypeKey"] != null)
                    {
                        Log.LogTypeKey = Convert.ToString(XmlNode.Attributes["LogTypeKey"].Value);
                    }
                    if (XmlNode.Attributes["LogCreateDate"] != null)
                    {
                        Log.LogCreateDate = Convert.ToDateTime(XmlNode.Attributes["LogCreateDate"].Value);
                    }
                    if (XmlNode.Attributes["LogCreateDateNum"] != null)
                    {
                        Log.LogCreateDateNum = long.Parse(XmlNode.Attributes["LogCreateDateNum"].Value);
                    }
                    if (XmlNode.Attributes["LogGUID"] != null)
                    {
                        Log.LogGUID = Convert.ToString(XmlNode.Attributes["LogGUID"].Value);
                    }
                    if (XmlNode.Attributes["LogUserID"] != null)
                    {
                        Log.LogUserID = Convert.ToInt32(XmlNode.Attributes["LogUserID"].Value);
                    }
                    if (XmlNode.Attributes["LogUserName"] != null)
                    {
                        Log.LogUserName = Convert.ToString(XmlNode.Attributes["LogUserName"].Value);
                    }
                    if (XmlNode.Attributes["LogPortalID"] != null)
                    {
                        Log.LogPortalID = Convert.ToInt32(XmlNode.Attributes["LogPortalID"].Value);
                    }
                    if (XmlNode.Attributes["LogPortalName"] != null)
                    {
                        Log.LogPortalName = Convert.ToString(XmlNode.Attributes["LogPortalName"].Value);
                    }
                    if (XmlNode.Attributes["LogFileID"] != null)
                    {
                        Log.LogFileID = Convert.ToString(XmlNode.Attributes["LogFileID"].Value);
                    }
                    if (XmlNode.Attributes["LogServerName"] != null)
                    {
                        Log.LogServerName = Convert.ToString(XmlNode.Attributes["LogServerName"].Value);
                    }
                    XmlNodeList xmlPropertyNodes = XmlNode.SelectNodes("properties/property");
                    try
                    {
                        refObjectHelperL1 = xmlPropertyNodes.GetEnumerator();
                        while (refObjectHelperL1.MoveNext())
                        {
                            string propertyValue;
                            XmlNode PropertyNode = (XmlNode) refObjectHelperL1.Current;
                            LogDetailInfo logDetails = new LogDetailInfo();
                            string propertyName = XmlUtils.GetNodeValue(PropertyNode, "name", "");
                            if (propertyName == "logdetail")
                            {
                                IEnumerator refObjectHelperL2=null;
                                XmlDocument xmlDetail = new XmlDocument();
                                xmlDetail.LoadXml(XmlUtils.GetNodeValue(PropertyNode, "value", ""));
                                try
                                {
                                    refObjectHelperL2 = xmlDetail.DocumentElement.ChildNodes.GetEnumerator();
                                    while (refObjectHelperL2.MoveNext())
                                    {
                                        XmlNode childNode = (XmlNode) refObjectHelperL2.Current;
                                        if (!childNode.HasChildNodes)
                                        {
                                            propertyName = childNode.Name;
                                            propertyValue = childNode.InnerText;
                                            logProp.Add(new LogDetailInfo(propertyName, propertyValue));
                                        }
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL2 is IDisposable)
                                    {
                                        (refObjectHelperL2 as IDisposable).Dispose();
                                    }
                                }
                            }
                            else
                            {
                                propertyValue = XmlUtils.GetNodeValue(PropertyNode, "value", "");
                                logProp.Add(new LogDetailInfo(propertyName, propertyValue));
                            }
                        }
                    }
                    finally
                    {
                        if (refObjectHelperL1 is IDisposable)
                        {
                            (refObjectHelperL1 as IDisposable).Dispose();
                        }
                    }
                    Log.LogProperties = logProp;
                    arrLog.Add(Log);
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return arrLog;
        }

        private LogTypeConfigInfo GetLogTypeConfig(string ConfigPortalID, string LogTypeKey)
        {
            XmlNode xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey='" + LogTypeKey + "' and @LogTypePortalID='" + ConfigPortalID + "']");
            if (xmlLogTypeInfo != null)
            {
                return this.GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
            }
            xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey='*' and @LogTypePortalID='" + ConfigPortalID + "']");
            if (xmlLogTypeInfo != null)
            {
                return this.GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
            }
            xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey='" + LogTypeKey + "' and @LogTypePortalID='*']");
            if (xmlLogTypeInfo != null)
            {
                return this.GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
            }
            xmlLogTypeInfo = xmlConfigDoc.SelectSingleNode("/LogConfig/LogTypeConfig[@LogTypeKey='*' and @LogTypePortalID='*']");
            if (xmlLogTypeInfo != null)
            {
                return this.GetLogTypeConfigInfoFromXML(xmlLogTypeInfo);
            }
            return null;
        }

        public override ArrayList GetLogTypeConfigInfo()
        {
            IEnumerator refObjectHelperL0=null;
            XmlNodeList xmlLogTypeConfigInfoList = this.GetConfigDoc().SelectNodes("/LogConfig/LogTypeConfig ");
            ArrayList arrLogTypeInfo = new ArrayList();
            try
            {
                refObjectHelperL0 = xmlLogTypeConfigInfoList.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    XmlNode xmlLogTypeConfigInfo = (XmlNode) refObjectHelperL0.Current;
                    arrLogTypeInfo.Add(this.GetLogTypeConfigInfoFromXML(xmlLogTypeConfigInfo));
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return arrLogTypeInfo;
        }

        public override LogTypeConfigInfo GetLogTypeConfigInfoByID(string ID)
        {
            XmlNode xmlLogTypeConfigInfo = this.GetConfigDoc().SelectSingleNode("/LogConfig/LogTypeConfig[@LogFileID='" + ID + "']");
            return this.GetLogTypeConfigInfoFromXML(xmlLogTypeConfigInfo);
        }

        private LogTypeConfigInfo GetLogTypeConfigInfoFromXML(XmlNode xmlLogTypeInfo)
        {
            LogTypeConfigInfo objLogTypeConfigInfo = new LogTypeConfigInfo();
            objLogTypeConfigInfo.LogFileNameWithPath = GetFilePath(xmlLogTypeInfo.Attributes["FileName"].Value);
            objLogTypeConfigInfo.LogFileName = xmlLogTypeInfo.Attributes["FileName"].Value;
            objLogTypeConfigInfo.LogTypeKey = xmlLogTypeInfo.Attributes["LogTypeKey"].Value;
            if (xmlLogTypeInfo.Attributes["LogTypePortalID"] != null)
            {
                objLogTypeConfigInfo.LogTypePortalID = xmlLogTypeInfo.Attributes["LogTypePortalID"].Value;
            }
            else
            {
                objLogTypeConfigInfo.LogTypePortalID = "*";
            }
            if (xmlLogTypeInfo.Attributes["KeepMostRecent"] != null)
            {
                objLogTypeConfigInfo.KeepMostRecent = xmlLogTypeInfo.Attributes["KeepMostRecent"].Value;
            }
            else
            {
                objLogTypeConfigInfo.KeepMostRecent = "*";
            }
            objLogTypeConfigInfo.ID = xmlLogTypeInfo.Attributes["LogFileID"].Value;
            objLogTypeConfigInfo.LoggingIsActive = Conversions.ToBoolean(Interaction.IIf(Strings.LCase(xmlLogTypeInfo.Attributes["LoggingStatus"].Value.ToString()) == "on", true, false));
            if (xmlLogTypeInfo.Attributes["EmailNotificationStatus"] != null)
            {
                objLogTypeConfigInfo.EmailNotificationIsActive = Conversions.ToBoolean(Interaction.IIf(Strings.LCase(xmlLogTypeInfo.Attributes["EmailNotificationStatus"].Value.ToString()) == "on", true, false));
            }
            else
            {
                objLogTypeConfigInfo.EmailNotificationIsActive = false;
            }
            if (xmlLogTypeInfo.Attributes["MailFromAddress"] != null)
            {
                objLogTypeConfigInfo.MailFromAddress = Convert.ToString(xmlLogTypeInfo.Attributes["MailFromAddress"].Value);
            }
            else
            {
                objLogTypeConfigInfo.MailFromAddress = Null.NullString;
            }
            if (xmlLogTypeInfo.Attributes["MailToAddress"] != null)
            {
                objLogTypeConfigInfo.MailToAddress = Convert.ToString(xmlLogTypeInfo.Attributes["MailToAddress"].Value);
            }
            else
            {
                objLogTypeConfigInfo.MailToAddress = Null.NullString;
            }
            if (xmlLogTypeInfo.Attributes["NotificationThreshold"] != null)
            {
                objLogTypeConfigInfo.NotificationThreshold = Convert.ToInt32(xmlLogTypeInfo.Attributes["NotificationThreshold"].Value);
            }
            else
            {
                objLogTypeConfigInfo.NotificationThreshold = -1;
            }
            if (xmlLogTypeInfo.Attributes["NotificationThresholdTime"] != null)
            {
                objLogTypeConfigInfo.NotificationThresholdTime = Convert.ToInt32(xmlLogTypeInfo.Attributes["NotificationThresholdTime"].Value);
            }
            else
            {
                objLogTypeConfigInfo.NotificationThresholdTime = -1;
            }
            if (xmlLogTypeInfo.Attributes["NotificationThresholdTimeType"] != null)
            {
                switch (Convert.ToString(xmlLogTypeInfo.Attributes["NotificationThresholdTimeType"].Value))
                {
                    case "1":
                        objLogTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Seconds;
                        return objLogTypeConfigInfo;

                    case "2":
                        objLogTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Minutes;
                        return objLogTypeConfigInfo;

                    case "3":
                        objLogTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Hours;
                        return objLogTypeConfigInfo;

                    case "4":
                        objLogTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.Days;
                        return objLogTypeConfigInfo;
                }
                objLogTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.None;
                return objLogTypeConfigInfo;
            }
            objLogTypeConfigInfo.NotificationThresholdTimeType = LogTypeConfigInfo.NotificationThresholdTimeTypes.None;
            return objLogTypeConfigInfo;
        }

        public override ArrayList GetLogTypeInfo()
        {
            IEnumerator refObjectHelperL0=null;
            XmlNodeList xmlLogTypeInfoList = this.GetConfigDoc().SelectNodes("/LogConfig/LogTypes/LogType");
            ArrayList arrLogTypeInfo = new ArrayList();
            try
            {
                refObjectHelperL0 = xmlLogTypeInfoList.GetEnumerator();
                while (refObjectHelperL0.MoveNext())
                {
                    XmlNode xmlLogTypeInfo = (XmlNode) refObjectHelperL0.Current;
                    arrLogTypeInfo.Add(this.GetLogTypeInfoFromXML(xmlLogTypeInfo));
                }
            }
            finally
            {
                if (refObjectHelperL0 is IDisposable)
                {
                    (refObjectHelperL0 as IDisposable).Dispose();
                }
            }
            return arrLogTypeInfo;
        }

        private LogTypeInfo GetLogTypeInfoFromXML(XmlNode xmlLogTypeInfo)
        {
            LogTypeInfo objLogTypeInfo = new LogTypeInfo();
            objLogTypeInfo.LogTypeKey = xmlLogTypeInfo.Attributes["LogTypeKey"].Value;
            objLogTypeInfo.LogTypeFriendlyName = xmlLogTypeInfo.Attributes["LogTypeFriendlyName"].Value;
            objLogTypeInfo.LogTypeDescription = xmlLogTypeInfo.Attributes["LogTypeDescription"].Value;
            objLogTypeInfo.LogTypeOwner = xmlLogTypeInfo.Attributes["LogTypeOwner"].Value;
            objLogTypeInfo.LogTypeCSSClass = xmlLogTypeInfo.Attributes["LogTypeCSSClass"].Value;
            return objLogTypeInfo;
        }

        public override object GetSingleLog(LogInfo objLogInfo, LoggingProvider.ReturnType objReturnType)
        {
            int intAttempts=0;
            XmlDocument xmlConfigDoc = this.GetConfigDoc();
            string strFileName = this.GetLogFileByLogFileID(this.GetConfigDoc(), objLogInfo.LogFileID);
            XmlDocument xmlDoc = new XmlDocument();
            while (!((xmlDoc.OuterXml != "") | (intAttempts == 100)))
            {
                intAttempts++;
                try
                {
                    xmlDoc.Load(strFileName);
                }
                catch (IOException exception1)
                {
    
                    IOException exc = exception1;
                    Thread.Sleep(1);
    
                }
            }
            XmlNode objxmlNode = xmlDoc.SelectSingleNode("/logs/log[@LogGUID='" + objLogInfo.LogGUID + "']");
            XmlDocument xmlDocOut = new XmlDocument();
            xmlDocOut.LoadXml("<SingleLog></SingleLog>");
            XmlNode xmlNewNode = xmlDocOut.ImportNode(objxmlNode, true);
            xmlDocOut.DocumentElement.AppendChild(xmlNewNode);
            if (objReturnType == LoggingProvider.ReturnType.XML)
            {
                return xmlDocOut.DocumentElement.SelectSingleNode("log");
            }
            int refIntHelperS0 = 0;
            return this.GetLogInfoFromXML(xmlDocOut, 0x7fffffff, 1, ref refIntHelperS0).GetItem(0);
        }

        private XmlElement GetXMLFromLogTypeConfigInfo(string LogFileID, bool IsActive, string LogTypeKey, string LogTypePortalID, string KeepMostRecent, string LogFileName, bool EmailNotificationIsActive, string NotificationThreshold, string NotificationThresholdTime, string NotificationThresholdTimeType, string MailFromAddress, string MailToAddress)
        {
            string BlankLogConfig = "\t<LogTypeConfig LogFileID=\"\" LoggingStatus=\"\" LogTypeKey=\"\" LogTypePortalID=\"\" KeepMostRecent=\"\" FileName=\"\" EmailNotificationStatus=\"\" NotificationThreshold=\"\" NotificationThresholdTime=\"\" NotificationThresholdTimeType=\"\" MailFromAddress=\"\" MailToAddress=\"\"/>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(BlankLogConfig);
            XmlElement xmlDocEl = xmlDoc.DocumentElement;
            xmlDocEl.Attributes["LogFileID"].Value = LogFileID;
            xmlDocEl.Attributes["LoggingStatus"].Value = Convert.ToString(RuntimeHelpers.GetObjectValue(Interaction.IIf(IsActive, "On", "Off")));
            xmlDocEl.Attributes["LogTypeKey"].Value = LogTypeKey;
            xmlDocEl.Attributes["LogTypePortalID"].Value = LogTypePortalID;
            xmlDocEl.Attributes["KeepMostRecent"].Value = KeepMostRecent;
            xmlDocEl.Attributes["FileName"].Value = LogFileName;
            xmlDocEl.Attributes["EmailNotificationStatus"].Value = Convert.ToString(RuntimeHelpers.GetObjectValue(Interaction.IIf(EmailNotificationIsActive, "On", "Off")));
            xmlDocEl.Attributes["NotificationThreshold"].Value = NotificationThreshold;
            xmlDocEl.Attributes["NotificationThresholdTime"].Value = NotificationThresholdTime;
            xmlDocEl.Attributes["NotificationThresholdTimeType"].Value = NotificationThresholdTimeType;
            xmlDocEl.Attributes["MailFromAddress"].Value = MailFromAddress;
            xmlDocEl.Attributes["MailToAddress"].Value = MailToAddress;
            return xmlDocEl;
        }

        private XmlElement GetXMLFromLogTypeInfo(string LogTypeKey, string LogTypeFriendlyName, string LogTypeDescription, string LogTypeCSSClass, string LogTypeOwner)
        {
            string BlankLogConfig = "<LogType LogTypeKey=\"\" LogTypeFriendlyName=\"\" LogTypeDescription=\"\" LogTypeOwner=\"\" LogTypeCSSClass=\"\"/>";
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(BlankLogConfig);
            XmlElement xmlDocEl = xmlDoc.DocumentElement;
            xmlDocEl.Attributes["LogTypeKey"].Value = LogTypeKey;
            xmlDocEl.Attributes["LogTypeFriendlyName"].Value = LogTypeFriendlyName;
            xmlDocEl.Attributes["LogTypeDescription"].Value = LogTypeDescription;
            xmlDocEl.Attributes["LogTypeOwner"].Value = LogTypeOwner;
            xmlDocEl.Attributes["LogTypeCSSClass"].Value = LogTypeCSSClass;
            return xmlDocEl;
        }

        public override bool LoggingIsEnabled(string LogType, int PortalID)
        {
            string ConfigPortalID = PortalID.ToString();
            if (PortalID == -1)
            {
                ConfigPortalID = "*";
            }
            LogTypeConfigInfo objLogTypeConfigInfo = this.GetLogTypeConfig(ConfigPortalID, LogType);
            return ((objLogTypeConfigInfo != null) && objLogTypeConfigInfo.LoggingIsActive);
        }

        public override void PurgeLogBuffer()
        {
            try
            {
                lockPurgeLog.AcquireWriterLock(0x2710);
                Collection c = LogQueue;
                int j = c.Count;
                int refIntHelperL0 = c.Count;
                for (int i = 1; i <= refIntHelperL0; i++)
                {
                    if (c[j] != null)
                    {
                        LogQueueItem objLogQueueItem = (LogQueueItem) c[j];
                        this.WriteLog(objLogQueueItem);
                    }
                    if (c[j] != null)
                    {
                        c.Remove(j);
                    }
                    j--;
                }
                ArrayList a = new XMLLoggingProvider().GetLogTypeConfigInfo();
                int refIntHelperL1 = a.Count - 1;
                for (int k = 0; k <= refIntHelperL1; k++)
                {
                    LogTypeConfigInfo objLogTypeConfigInfo = (LogTypeConfigInfo) a[k];
                    if ((((objLogTypeConfigInfo.KeepMostRecent != "*") && (objLogTypeConfigInfo.LogFileName != "")) ? 1 : 0) != 0)
                    {
                        bool FileExists=false;
                        XmlDocument xmlLog = new XmlDocument();
                        try
                        {
                            int intAttempts=0;
                            while (!((xmlLog.OuterXml != "") | (intAttempts == 100)))
                            {
                                intAttempts++;
                                try
                                {
                                    xmlLog.Load(objLogTypeConfigInfo.LogFileNameWithPath);
                                    FileExists = true;
                                }
                                catch (IOException exception1)
                                {
                    
                                    IOException exc = exception1;
                                    Thread.Sleep(1);
                    
                                }
                            }
                        }
                        catch (FileNotFoundException exception2)
                        {
                            ProjectData.SetProjectError(exception2);
                            FileNotFoundException exc = exception2;
                            FileExists = false;
            
                        }
                        catch (XmlException exception3)
                        {
                            ProjectData.SetProjectError(exception3);
                            XmlException exc = exception3;
                            FileExists = false;
            
                        }
                        if (FileExists)
                        {
                            XmlNodeList objTotalNodes;
                            if (objLogTypeConfigInfo.LogTypePortalID == "*")
                            {
                                objTotalNodes = xmlLog.DocumentElement.SelectNodes("log[@LogTypeKey='" + objLogTypeConfigInfo.LogTypeKey + "']");
                            }
                            else
                            {
                                objTotalNodes = xmlLog.DocumentElement.SelectNodes("log[@LogTypeKey='" + objLogTypeConfigInfo.LogTypeKey + "' and LogPortalID='" + objLogTypeConfigInfo.LogTypePortalID + "']");
                            }
                            int intNodeCount = objTotalNodes.Count;
                            int intKeepMostRecent = Convert.ToInt32(objLogTypeConfigInfo.KeepMostRecent);
                            if (intNodeCount > intKeepMostRecent)
                            {
                                IEnumerator refObjectHelperL0=null;
                                int m = 0;
                                try
                                {
                                    refObjectHelperL0 = objTotalNodes.GetEnumerator();
                                    while (refObjectHelperL0.MoveNext())
                                    {
                                        XmlNode objTotalNode = (XmlNode) refObjectHelperL0.Current;
                                        if ((intNodeCount - m) > intKeepMostRecent)
                                        {
                                            xmlLog.DocumentElement.RemoveChild(objTotalNode);
                                        }
                                        m++;
                                    }
                                }
                                finally
                                {
                                    if (refObjectHelperL0 is IDisposable)
                                    {
                                        (refObjectHelperL0 as IDisposable).Dispose();
                                    }
                                }
                                xmlLog.Save(objLogTypeConfigInfo.LogFileNameWithPath);
                            }
                            else
                            {
                                xmlLog = null;
                            }
                        }
                    }
                }
            }
            finally
            {
                lockPurgeLog.ReleaseWriterLock();
            }
        }

        public override void SendLogNotifications()
        {
            try
            {
                lockNotif.AcquireWriterLock(0x2710);
                XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
                try
                {
                    xmlPendingNotificationsDoc.Load(GetFilePath("PendingLogNotifications.xml.resources"));
                }
                catch (FileNotFoundException exception1)
                {
    
                    FileNotFoundException exc = exception1;
    
                    return;
    
                }
                XMLLoggingProvider x = new XMLLoggingProvider();
                ArrayList arrLogTypeInfo = x.GetLogTypeConfigInfo();
                this.PurgeLogBuffer();
                int refIntHelperL0 = arrLogTypeInfo.Count - 1;
                for (int a = 0; a <= refIntHelperL0; a++)
                {
                    LogTypeConfigInfo objLogTypeInfo = (LogTypeConfigInfo) arrLogTypeInfo[a];
                    if (objLogTypeInfo.EmailNotificationIsActive)
                    {
                        XmlNodeList xmlPendingNotifications = xmlPendingNotificationsDoc.DocumentElement.SelectNodes("log[@NotificationLogTypeKey='" + objLogTypeInfo.LogTypeKey + "' and @LogTypePortalID='" + objLogTypeInfo.LogTypePortalID + "' and number(@LogCreateDateNum) > " + this.DateToNum(objLogTypeInfo.StartDateTime).ToString() + "]");
                        if (xmlPendingNotifications.Count >= objLogTypeInfo.NotificationThreshold)
                        {
                            IEnumerator refObjectHelperL0=null;
                            XmlDocument xmlOut = new XmlDocument();
                            xmlOut.LoadXml("<notification></notification>");
                            try
                            {
                                refObjectHelperL0 = xmlPendingNotifications.GetEnumerator();
                                while (refObjectHelperL0.MoveNext())
                                {
                                    XmlNode xmlPendingNotification = (XmlNode) refObjectHelperL0.Current;
                                    XmlNode tmpNode = xmlOut.ImportNode(xmlPendingNotification, true);
                                    xmlOut.DocumentElement.AppendChild(tmpNode);
                                    xmlPendingNotificationsDoc.DocumentElement.RemoveChild(xmlPendingNotification);
                                }
                            }
                            finally
                            {
                                if (refObjectHelperL0 is IDisposable)
                                {
                                    (refObjectHelperL0 as IDisposable).Dispose();
                                }
                            }
                            bool NotificationFailed = false;
                            string errSendNotif = DotNetNuke.Services.Mail.Mail.SendMail(objLogTypeInfo.MailFromAddress, objLogTypeInfo.MailToAddress, "", "Log Notification", xmlOut.OuterXml, "", "", "", "", "", "");
                            if (errSendNotif != "")
                            {
                                NotificationFailed = true;
                            }
                            EventLogController objEventLogController = new EventLogController();
                            if (NotificationFailed)
                            {
                                LogInfo objEventLogInfo = new LogInfo();
                                objEventLogInfo.LogTypeKey = EventLogController.EventLogType.LOG_NOTIFICATION_FAILURE.ToString();
                                objEventLogInfo.AddProperty("Log Notification Failed: ", errSendNotif);
                                objEventLogController.AddLog(objEventLogInfo);
                                xmlPendingNotificationsDoc.Load(GetFilePath("PendingLogNotifications.xml.resources"));
                                if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"] == null)
                                {
                                    XmlAttribute xmlNotificationFailed = xmlPendingNotificationsDoc.CreateAttribute("LastNotificationFailure");
                                    xmlNotificationFailed.Value = DateTime.Now.ToString();
                                    xmlPendingNotificationsDoc.DocumentElement.Attributes.Append(xmlNotificationFailed);
                                }
                                else
                                {
                                    xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"].Value = DateTime.Now.ToString();
                                }
                                xmlPendingNotificationsDoc.Save(GetFilePath("PendingLogNotifications.xml.resources"));
                            }
                            else
                            {
                                if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"] != null)
                                {
                                    xmlPendingNotificationsDoc.DocumentElement.Attributes.Remove(xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationFailure"]);
                                }
                                if (xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"] == null)
                                {
                                    XmlAttribute xmlNotificationSucceeded = xmlPendingNotificationsDoc.CreateAttribute("LastNotificationSuccess");
                                    xmlNotificationSucceeded.Value = DateTime.Now.ToString();
                                    xmlPendingNotificationsDoc.DocumentElement.Attributes.Append(xmlNotificationSucceeded);
                                }
                                else
                                {
                                    xmlPendingNotificationsDoc.DocumentElement.Attributes["LastNotificationSuccess"].Value = DateTime.Now.ToString();
                                }
                                xmlPendingNotificationsDoc.Save(GetFilePath("PendingLogNotifications.xml.resources"));
                            }
                        }
                    }
                }
                x.DeleteOldPendingNotifications();
            }
            catch (Exception exception2)
            {
                ProjectData.SetProjectError(exception2);
                Exception exc = exception2;
                DotNetNuke.Services.Exceptions.Exceptions.LogException(exc);

            }
            finally
            {
                lockNotif.ReleaseWriterLock();
            }
        }

        public override bool SupportsEmailNotification()
        {
            return true;
        }

        public override bool SupportsInternalViewer()
        {
            return true;
        }

        public override bool SupportsSendToCoreTeam()
        {
            return true;
        }

        public override bool SupportsSendViaEmail()
        {
            return true;
        }

        public override void UpdateLogType(string LogTypeKey, string LogTypeFriendlyName, string LogTypeDescription, string LogTypeCSSClass, string LogTypeOwner)
        {
            this.DeleteLogType(LogTypeKey);
            this.AddLogType(LogTypeKey, LogTypeFriendlyName, LogTypeDescription, LogTypeCSSClass, LogTypeOwner);
        }

        public override void UpdateLogTypeConfigInfo(string ID, bool IsActive, string LogTypeKey, string LogTypePortalID, string KeepMostRecent, string LogFileName, bool EmailNotificationIsActive, string NotificationThreshold, string NotificationThresholdTime, string NotificationThresholdTimeType, string MailFromAddress, string MailToAddress)
        {
            this.DeleteLogTypeConfigInfo(ID);
            this.AddLogTypeConfigInfo(ID, IsActive, LogTypeKey, LogTypePortalID, KeepMostRecent, LogFileName, EmailNotificationIsActive, NotificationThreshold, NotificationThresholdTime, NotificationThresholdTimeType, MailFromAddress, MailToAddress);
        }

        private void WriteLog(LogQueueItem objLogQueueItem)
        {
            FileStream fs = null;
            StreamWriter sw = null;
            LogTypeConfigInfo objLogTypeConfigInfo = objLogQueueItem.LogTypeConfigInfo;
            string LogString = objLogQueueItem.LogString;
            try
            {
                if (objLogTypeConfigInfo.LogFileNameWithPath != "")
                {
                    int intAttempts=0;
                    lockLog.AcquireWriterLock(0x2710);
                    while (!((fs != null) | (intAttempts == 100)))
                    {
                        intAttempts++;
                        try
                        {
                            fs = new FileStream(objLogTypeConfigInfo.LogFileNameWithPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                        }
                        catch (IOException exception1)
                        {
            
                            IOException exc = exception1;
                            Thread.Sleep(1);
            
                        }
                    }
                    if (fs == null)
                    {
                        if (HttpContext.Current != null)
                        {
                            HttpContext.Current.Response.Write("An error has occurred writing to the exception log.");
                            HttpContext.Current.Response.End();
                        }
                    }
                    else
                    {
                        sw = new StreamWriter(fs, Encoding.UTF8);
                        long FileLength = fs.Length;
                        if (FileLength > 0L)
                        {
                            fs.Position = FileLength - 9L;
                        }
                        else
                        {
                            LogString = "<logs>" + LogString;
                        }
                        sw.WriteLine(LogString + "</logs>");
                        sw.Flush();
                    }
                    if (sw != null)
                    {
                        sw.Close();
                    }
                    if (fs != null)
                    {
                        fs.Close();
                    }
                }
                if (objLogTypeConfigInfo.EmailNotificationIsActive)
                {
                    try
                    {
                        lockNotif.AcquireWriterLock(0x2710);
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.LoadXml(LogString);
                        if (objLogTypeConfigInfo.NotificationThreshold == 0)
                        {
                            DotNetNuke.Services.Mail.Mail.SendMail(objLogTypeConfigInfo.MailFromAddress, objLogTypeConfigInfo.MailToAddress, "", "Event Notification", xmlDoc.InnerXml, "", "", "", "", "", "");
                        }
                        else if (objLogTypeConfigInfo.LogTypeKey != "LOG_NOTIFICATION_FAILURE")
                        {
                            XmlDocument xmlPendingNotificationsDoc = new XmlDocument();
                            try
                            {
                                xmlPendingNotificationsDoc.Load(GetFilePath("PendingLogNotifications.xml.resources"));
                            }
                            catch (FileNotFoundException exception2)
                            {
                                ProjectData.SetProjectError(exception2);
                                FileNotFoundException exc = exception2;
                                xmlPendingNotificationsDoc.LoadXml("<PendingNotifications></PendingNotifications>");
                
                            }
                            XmlNode xmlLogNode = xmlPendingNotificationsDoc.ImportNode(xmlDoc.FirstChild, true);
                            XmlAttribute xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute("MailFromAddress");
                            xmlAttrib.Value = Convert.ToString(objLogTypeConfigInfo.MailFromAddress);
                            xmlLogNode.Attributes.Append(xmlAttrib);
                            xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute("NotificationLogTypeKey");
                            xmlAttrib.Value = Convert.ToString(objLogTypeConfigInfo.LogTypeKey);
                            xmlLogNode.Attributes.Append(xmlAttrib);
                            xmlAttrib = xmlPendingNotificationsDoc.CreateAttribute("LogTypePortalID");
                            if (objLogTypeConfigInfo.LogTypePortalID == "-1")
                            {
                                xmlAttrib.Value = "*";
                            }
                            else
                            {
                                xmlAttrib.Value = objLogTypeConfigInfo.LogTypePortalID;
                            }
                            xmlLogNode.Attributes.Append(xmlAttrib);
                            XmlElement x = xmlPendingNotificationsDoc.CreateElement("EmailAddress");
                            x.InnerText = Convert.ToString(objLogTypeConfigInfo.MailToAddress);
                            xmlLogNode.AppendChild(x);
                            xmlPendingNotificationsDoc.DocumentElement.AppendChild(xmlLogNode);
                            xmlPendingNotificationsDoc.Save(GetFilePath("PendingLogNotifications.xml.resources"));
                        }
                    }
                    finally
                    {
                        lockNotif.ReleaseWriterLock();
                    }
                }
            }
            catch (UnauthorizedAccessException exception3)
            {
                ProjectData.SetProjectError(exception3);
                UnauthorizedAccessException exc = exception3;
                if (HttpContext.Current != null)
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader(response, "Unauthorized Access Error");
                    string strMessage = exc.Message + " The Windows User Account listed below must have Read/Write Privileges to this path.";
                    HtmlUtils.WriteError(response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage);
                    HtmlUtils.WriteFooter(response);
                    response.End();
                }

            }
            catch (DirectoryNotFoundException exception4)
            {
                ProjectData.SetProjectError(exception4);
                DirectoryNotFoundException exc = exception4;
                if (HttpContext.Current != null)
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader(response, "Directory Not Found Error");
                    string strMessage = exc.Message;
                    HtmlUtils.WriteError(response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage);
                    HtmlUtils.WriteFooter(response);
                    response.End();
                }

            }
            catch (PathTooLongException exception5)
            {
                ProjectData.SetProjectError(exception5);
                PathTooLongException exc = exception5;
                if (HttpContext.Current != null)
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader(response, "Path Too Long Error");
                    string strMessage = exc.Message;
                    HtmlUtils.WriteError(response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage);
                    HtmlUtils.WriteFooter(response);
                    response.End();
                }

            }
            catch (IOException exception6)
            {
                ProjectData.SetProjectError(exception6);
                IOException exc = exception6;
                if (HttpContext.Current != null)
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader(response, "IO Error");
                    string strMessage = exc.Message;
                    HtmlUtils.WriteError(response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage);
                    HtmlUtils.WriteFooter(response);
                    response.End();
                }

            }
            catch (Exception exception7)
            {
                ProjectData.SetProjectError(exception7);
                Exception exc = exception7;
                if (HttpContext.Current != null)
                {
                    HttpResponse response = HttpContext.Current.Response;
                    HtmlUtils.WriteHeader(response, "Unhandled Error");
                    string strMessage = exc.Message;
                    HtmlUtils.WriteError(response, objLogTypeConfigInfo.LogFileNameWithPath, strMessage);
                    HtmlUtils.WriteFooter(response);
                    response.End();
                }

            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                if (fs != null)
                {
                    fs.Close();
                }
                lockLog.ReleaseWriterLock();
            }
        }
    }
}

